
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;


import lexer.Token;
import parser.Parser;
import parser.SyntacticTree;
import parser.SyntaxTranslator;
import parser.SyntacticTreeTranslator;

import parser.lr.LRParser;

import reggex.Reggex;
import reggex.ReggexAttributes;

import parser.exception.ParsingException;

import automaton.NDState;
import automaton.NondeterministicAutomaton;


public class Hola {
	
	public static void main(String[] args) {
		if(args.length < 2)
			throw new RuntimeException("Se necesitan dos parámetros");
		
		new Hola().match(args[0], args[1]);
	}
	
	
	public void run(String arg) {
		Parser p = Reggex.parser();
		
		try {
			Iterator<Character> input = getIter(arg);
			Iterator<Token> tokens = Reggex.tokenizer().getTokens(input);
			SyntaxTranslator<SyntacticTree> syntax = new SyntacticTreeTranslator();
			SyntacticTree tree = p.parse(tokens, syntax);
			
			System.out.println(tree);
			
		} catch(ParsingException e) {
			System.out.println("Uy, hubo errores");
		}
	}
	
	Iterator<Character> getIter(String str) {
		List<Character> l = new ArrayList<Character>(str.length());
		for(int i = 0; i < str.length(); i++)
			l.add(str.charAt(i));
			
		return l.iterator();
	}
	
	
	public void match(String reggex, String line) {
		Parser p = Reggex.parser();
		
		Iterator<Character> input = getIter(reggex);
		Iterator<Token> tokens = Reggex.tokenizer().getTokens(input);
		
		SyntaxTranslator<ReggexAttributes> syntax = Reggex.translator();
		
		try {
			NondeterministicAutomaton<Character> a = p.parse(tokens, syntax).getAutomaton();
			System.out.println(a.accepts(getIter(line)));
		} catch(ParsingException e) {
			System.out.println("Uy, hubo errores");
		}
	}
}

